import 'package:agora_rtc_engine/agora_rtc_engine.dart';
import 'package:flutter/material.dart';
import 'package:gameplay_flutter/m_chatroom/src/rtc/rtm_mixin.dart';

import '../../rtc/agora_rtc.dart';
import '../../rtc/agora_rtm.dart';
import '../../rtc/rtc_mixin.dart';

class RoomRtcRtm with RtcMixin, RtmMixin {
  late RtcMixin impl;
  late RtmMixin implRtm;

  RoomRtcRtm({
    required RtcConfig rtcConfig,
    required ValueNotifier<Set<int>> speakers,
  }) {
    this.rtcConfig = rtcConfig;
    this.speakers = speakers;

    // impl = rtcConfig.rtcType == 2 ? ZegoRtc() : AgoraRtc();
    impl = AgoraRtc();
    impl.rtcConfig = rtcConfig;
    impl.speakers = speakers;

    implRtm = AgoraRtm();
    implRtm.rtmConfig = rtcConfig;
  }

  /// =========  rtm 相关 ================
  @override
  Future<void> initRtm() => implRtm.initRtm();

  @override
  Future<void> loginRtm(String token) => implRtm.loginRtm(token);

  @override
  Future<void> publishRtm(String msg) => implRtm.publishRtm(msg);

  @override
  Future<void> subscribeRtm() => implRtm.subscribeRtm();

  @override
  Future<void> leaveRtmChannel() => implRtm.leaveRtmChannel();

  /// =========  rtc 相关 ================
  @override
  Future<void> init() => impl.init();

  @override
  Future<void> joinRoom() => impl.joinRoom();

  @override
  Future<void> leaveChannel() => impl.leaveChannel();

  @override
  Future<void> openMic() => impl.openMic();

  @override
  Future<void> closeMic() => impl.closeMic();

  @override
  Future<void> setBroadcaster() => impl.setBroadcaster();

  @override
  Future<void> setAudience() => impl.setAudience();

  @override
  bool get isBroadcaster => impl.isBroadcaster;

  @override
  Future<void> startPlaySong(String songCode) => impl.startPlaySong(songCode);

  @override
  MusicContentCenter? get musicCenter => impl.musicCenter;

  @override
  set musicPlayerId(int id) => impl.musicPlayerId = id;
}

class RtcConfig {
  static const rtcTypeAgora = 1;

  /// rtc type
  final int rtcType;

  final String rtmToken;
  final String rtcToken;

  /// 频道ID(房间id)
  final String channelId;

  /// 声网极速直播模式
  final bool lowLatency;

  /// 即构推流id
  final String publishStream;

  RtcConfig({
    this.rtcType = rtcTypeAgora,
    required this.rtmToken,
    required this.rtcToken,
    required this.channelId,
    this.lowLatency = false,
    this.publishStream = '',
  });
}
